package net.vsame.url2sql.web;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.vsame.url2sql.dao.JdbcUtils;
import net.vsame.url2sql.domain.InterceptorConfig;
import net.vsame.url2sql.domain.UrlConfig;
import net.vsame.url2sql.helper.FileUtils;

import com.alibaba.fastjson.JSON;

public class UrlSqlMappingHelper {
	
	private static Map<String, UrlConfig> urlSqlMap = new HashMap<String, UrlConfig>();
	private static List<InterceptorConfig> interceptorConfigs = new ArrayList<InterceptorConfig>();
	
	/**
	 * 根据Url取得Sql
	 * @param url
	 * @return
	 */
	public static UrlConfig getConfig(String url){
		return urlSqlMap.get(url);
	}
	
	public static void addUrlConfig(String url, UrlConfig urlConfig){
		urlSqlMap.put(url, urlConfig);
	}
	public static void addInterceptorConfig(String regex, String js, int status){
		interceptorConfigs.add(new InterceptorConfig(regex, js, status));
	}
	public static void addInterceptorConfig(String regex, String js){
		addInterceptorConfig(regex, js, 1);
	}
	public static Map<String, UrlConfig> getUrlSqlMap() {
		return urlSqlMap;
	}
	public static List<InterceptorConfig> getInterceptorConfigs() {
		return interceptorConfigs;
	}
	
	//初始化表
	public static void init(){
		List<String> list = JdbcUtils.getAllTables();
		if(!list.contains("__url2sqlmapping")){
			//执行创建表语名
			InputStream in = UrlSqlMappingHelper.class.getClassLoader().getResourceAsStream("/init.sql");
			System.out.println(in);
			String[] sqls = FileUtils.readAllText(in).split(";");
			for(String sql : sqls){
				System.out.println(sql);
				JdbcUtils.execute(sql);
			}
		}
	}
	
	public static void reload(){
		urlSqlMap.clear();
		interceptorConfigs.clear();
		initInterceptor();
		initUrlConfig();
	}
	
	/**
	 * 初始化拦截器
	 */
	@SuppressWarnings("rawtypes")
	private static void initInterceptor(){
		InputStream in = Url2SqlFilter.class.getClassLoader().getResourceAsStream("rootInterceptor.json");
		String json = FileUtils.readAllText(in);
		List<Map> array = JSON.parseArray(json, Map.class);
		for(Map map : array){
			addInterceptorConfig(getString(map.get("regex")), getString(map.get("js")));
		}
		//查询表
		List<Map<String, Object>> list = JdbcUtils.queryList("SELECT * from __url2sqltable");
		for(Map map : list){
			addInterceptorConfig(getString(map.get("regex")), getString(map.get("js")), (Integer) map.get("status"));
		}
	}
	
	private static String getString(Object o){
		return o==null ? null : o+"";
	}
	
	/**
	 * 初始化URL
	 */
	@SuppressWarnings("rawtypes")
	private static void initUrlConfig() {
		InputStream in = Url2SqlFilter.class.getClassLoader().getResourceAsStream("rootSql.json");
		String json = FileUtils.readAllText(in);
		List<Map> array = JSON.parseArray(json, Map.class);
		for(Map map : array){
			UrlConfig urlConfig = new UrlConfig();
			urlConfig.setUrl(getString(map.get("path")));
			urlConfig.setSql(getString(map.get("sql")));
			urlConfig.setJs(getString(map.get("js")));
			addUrlConfig("/root/" + urlConfig.getUrl(), urlConfig);
		}
		//查询表
		List<Map<String, Object>> list = JdbcUtils.queryList("SELECT sm.js,sm.v, sm.path,sm.`sql`,sm.`status`, t.`name`,t.`status` tableStatus from __url2sqlmapping sm, __url2sqltable t WHERE sm.tableId=t.id");
		for(Map map : list){
			UrlConfig urlConfig = new UrlConfig();
			urlConfig.setUrl(getString(map.get("path")));
			urlConfig.setSql(getString(map.get("sql")));
			urlConfig.setJs(getString(map.get("js")));
			urlConfig.setV(getString(map.get("v")));
			int status = (Integer) map.get("status");
			int tableStatus = (Integer) map.get("tableStatus");
			urlConfig.setStatus(status|tableStatus);
			String tableName = getString(map.get("name"));
			addUrlConfig("/" + tableName + "/" + urlConfig.getUrl(), urlConfig);
		}
	}

}
